package com.rural.bus;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import cn.jpush.android.api.JPushInterface;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.rural.bus.model.User;
import com.rural.bus.utils.ActivityControl;
import com.rural.bus.utils.BitmapHelp;
import com.rural.bus.utils.FileUtils;
import com.rural.bus.view.PlusImageView;

/**
 * base activity
 *
 */
public abstract class BaseActivity extends Activity {
	protected View mTitleView;
	protected Button mBtLeft;
	protected Button mBtMiddle;
	public Button mBtRight;
	protected PlusImageView mImgLeft;
	protected PlusImageView mImgRight;
	protected BitmapUtils bitmapUtils;
	protected String dir;
	protected BitmapDisplayConfig config;
	protected DbUtils dbUtils;
	protected LayoutInflater mInflater;
	protected int screenWidth;
	protected int screenHeight;
	
	private View titleView;
	protected View titleSplitView;
	
	public User mCurrentUser;
	
	public View getTitleView() {
		return titleView;
	}

	/** Activity 生命周期  **/
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		mInflater = LayoutInflater.from(this);
		bitmapUtils=BitmapHelp.getBitmapUtils(this);
		config=BitmapHelp.getDisplayConfig(this, 50, 50);
		dbUtils=DbUtils.create(this);
		dir=FileUtils.getSDCardRoot()+getPackageName()+File.separator;
		FileUtils.mkdirs(dir);
		ActivityControl.getInstance().addActivity(this);
		
		WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
		screenWidth= wm.getDefaultDisplay().getWidth();
		screenHeight= wm.getDefaultDisplay().getHeight();
		
		initCurrentUser();
 
	}

	@Override
	public void setContentView(int layoutResID) {
		View view = mInflater.inflate(layoutResID, null);
		setContentView(view,true);
	}
	
	protected void setContentView(int layoutResID, boolean showTitle) {
		View view = mInflater.inflate(layoutResID, null);
		setContentView( view,showTitle);
	}
		
	
	@Override
	public void setContentView(View view) {
		setContentView(view,true);
	}
	
	protected void setContentView(View view, boolean showTitle){
		LinearLayout rootView = new LinearLayout(this);
		rootView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		rootView.setOrientation(LinearLayout.VERTICAL);

		if (showTitle) {
			titleView = mInflater.inflate(R.layout.activity_base_title, null);
			rootView.addView(titleView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			mBtLeft = (Button) titleView.findViewById(R.id.button_left);
			mBtRight = (Button) titleView.findViewById(R.id.button_right);
			mBtMiddle = (Button) titleView.findViewById(R.id.button_middle);
			mImgLeft = (PlusImageView) titleView.findViewById(R.id.image_left);
			mImgRight = (PlusImageView) titleView.findViewById(R.id.image_right);
			mTitleView = titleView;
			titleView.setVisibility(View.VISIBLE);
		}
		rootView.addView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		super.setContentView(rootView);

//		initView();
//		setListeners();
//		initData();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		ActivityControl.getInstance().removeActivity(this);
	}
	
	/** 自定义方法 **/
	
	/** 初始化view, 该方法做findViewById, setOnClickListener 事情 */
	protected abstract void initView();
	
	/** 初始view数据, 做setText, setAdapter 事情*/
	protected abstract void initData();
	
	/** toast **/
	protected void toast(int msgId) {
		toast(getString(msgId));
	}
	
	/** toast **/
	public void toast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
	}
	
	//原来位置 得到高度为0
	protected int getTitleHeight(){
		return mTitleView.getHeight();
	}
	
	/**
	 * 跳转
	 * @param c
	 */
	protected void startActivity(Class c) {
	    Intent intent=new Intent(this, c);
		startActivity(intent);
	}
	
	protected void startActivity(Class c,Bundle b) {
		Intent intent=new Intent(this, c);
		intent.putExtras(b);
		startActivity(intent);
	}
	
	protected abstract void setListeners();
	
	@Override
	protected void onResume() {
		super.onResume();
		JPushInterface.onResume(this);
		initCurrentUser();
//		String regionId=JPushInterface.getRegistrationID(this);
//		Log.d("majie", regionId);
	}
	
	private void initCurrentUser(){
		MyApplication application=(MyApplication) getApplication();
		mCurrentUser=application.getCurrentUser();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		JPushInterface.onPause(this);
		
	}
	
}